# -*- coding: utf-8 -*-
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.http import Http404, HttpResponseRedirect
from friends.models import FriendshipRequest
from main.support import has_user_info_if_authenticated

@login_required
@has_user_info_if_authenticated
def invite(request, user_id):
    try:
        user = User.objects.get(id=user_id)
    except:
        raise Http404
    me = request.user
    is_friends = me in user.user_info.friends.all()
    invited = bool(FriendshipRequest.objects.filter(from_user=me, to_user=user))
    meinvited = bool(FriendshipRequest.objects.filter(from_user=user, to_user=me))
    if is_friends or invited or meinvited:
        if meinvited:
            if invited:
                r2 = FriendshipRequest.objects.get(from_user=me, to_user=user)
                r2.delete()
            r = FriendshipRequest.objects.get(from_user=user, to_user=me)
            r.accept()
    else:
        r = FriendshipRequest(from_user=me, to_user=user, message="Privet")
        r.save()
    try:
        user.notify_settings.all()[0].send_notify_friends(me)
    except:
        pass
    return HttpResponseRedirect("/profile/%d/" % user.id)


@login_required
@has_user_info_if_authenticated
def accept(request, user_id):
    try:
        user = User.objects.get(id=user_id)
        #u = UserInfo.objects.exclude(user=request.user).filter(user__is_active=True).get(user=user)
    except:
        raise Http404
    me = request.user
    is_friends = user in me.user_info.friends.all()
    invited = bool(FriendshipRequest.objects.filter(from_user=me, to_user=user))
    meinvited = bool(FriendshipRequest.objects.filter(from_user=user, to_user=me))
    if not is_friends and meinvited:
        if invited:
            r2 = FriendshipRequest.objects.get(from_user=me, to_user=user)
            r2.delete()
        r = FriendshipRequest.objects.get(from_user=user, to_user=me)
        r.accept()
    else:
        pass
    return HttpResponseRedirect("/profile/%d/" % user.id)


@login_required
@has_user_info_if_authenticated
def decline(request, user_id):
    try:
        user = User.objects.get(id=user_id)
    except:
        raise Http404
    me = request.user
    invited = bool(FriendshipRequest.objects.filter(from_user=me, to_user=user))
    meinvited = bool(FriendshipRequest.objects.filter(from_user=user, to_user=me))
    if meinvited:
        r = FriendshipRequest.objects.get(from_user=user, to_user=me)
        r.delete()
    if invited:
        r2 = FriendshipRequest.objects.get(from_user=me, to_user=user)
        r2.delete()
    return HttpResponseRedirect("/profile/%d/" % user.id)


@login_required
@has_user_info_if_authenticated
def cancel(request, user_id):
    try:
        user = User.objects.get(id=user_id)
    except:
        raise Http404
    me = request.user
    is_friends = bool(user in me.user_info.friends.all())
    if is_friends:
        me.user_info.friends.remove(user)
        user.user_info.friends.remove(me)
    else:
        raise Http404
    return HttpResponseRedirect("/profile/%d/" % user.id)
